package com.banmao.mall.controller.admin;

import com.banmao.mall.model.bo.admin.advertising.AdCreatBO;
import com.banmao.mall.model.pojo.Ad;
import com.banmao.mall.model.pojo.Coupon;
import com.banmao.mall.model.pojo.Topic;
import com.banmao.mall.model.vo.BaseRespVO;
import com.banmao.mall.model.vo.admin.promote.advertising.AdCreateVO;
import com.banmao.mall.model.vo.admin.promote.advertising.AdListVO;
import com.banmao.mall.model.vo.admin.promote.coupons.CouponListUserVO;
import com.banmao.mall.model.vo.admin.promote.coupons.CouponListVO;
import com.banmao.mall.model.vo.admin.promote.topic.TopicCreateVO;
import com.banmao.mall.model.vo.admin.promote.topic.TopicListVO;
import com.banmao.mall.model.vo.client.index.BannerVO;
import com.banmao.mall.service.admin.AdService;
import net.sf.jsqlparser.statement.select.Top;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/admin")
public class AdController {
    @Autowired
    AdService adService;
    //-----------广告管理-----------------
    //获取广告列表


    @RequestMapping("/ad/list")
    @RequiresPermissions("admin:ad:list")
    public BaseRespVO adList(Integer page, Integer limit, String sort, String order,String name,String content){
        AdListVO adListVO = adService.selectAdlist(page,limit,sort,order,name,content);
        return BaseRespVO.ok(adListVO);
    }

    //添加广告
    @RequestMapping("/ad/create")
    @RequiresPermissions("admin:ad:create")
    public BaseRespVO adCreate(@RequestBody AdCreatBO adCreatBO){

        AdCreateVO adCreateVO = adService.adCreate(adCreatBO);
        return BaseRespVO.ok(adCreateVO);
    }

    //编辑广告
    @RequestMapping("/ad/update")
    @RequiresPermissions("admin:ad:update")
    public  BaseRespVO update(@RequestBody Ad ad){
        int result = adService.updateAd(ad);
        if (result == 200){
            return BaseRespVO.ok(ad);
        }else if (result == 404){
            return BaseRespVO.error("系统升级中",404);
        }else{
            return BaseRespVO.error("系统繁忙",500);
        }
    }

    //删除广告
    @RequestMapping("/ad/delete")
    @RequiresPermissions("admin:ad:delete")
    public BaseRespVO delete(@RequestBody Ad ad){
        int result = adService.delete(ad);
        if (result == 200){
            return BaseRespVO.ok(ad);
        }else if (result == 404){
            return BaseRespVO.error("系统升级中",404);
        }else{
            return BaseRespVO.error("系统繁忙",500);
        }
    }



    //--------------优惠卷管理-----------------
    //获取优惠卷列表
    @RequestMapping("/coupon/list")
    @RequiresPermissions("admin:coupon:list")
    public BaseRespVO couponList(Integer page,Integer limit,String sort,String order,String name,Integer type,Integer status){
        CouponListVO couponListVO = adService.couponList(page,limit,sort,order,name,type,status);
        return BaseRespVO.ok(couponListVO);
    }
    //添加优惠卷
    //TODO 报错 已解决 string[] 数组问题
    @RequestMapping("/coupon/create")
    @RequiresPermissions("admin:coupon:create")
    public BaseRespVO couponCreate(@RequestBody Coupon coupon){
        Coupon coupon1 = adService.couponCreate(coupon);
        return BaseRespVO.ok(coupon1);
    }

    //更新优惠卷
    @RequestMapping("/coupon/update")
    @RequiresPermissions("admin:coupon:update")
    public BaseRespVO couponUpdate(@RequestBody Coupon coupon){
        Coupon coupon1 = adService.couponUpdate(coupon);
        return BaseRespVO.ok(coupon1);
    }

    //详情listuser listuser?page=1&limit=20&couponId=44&sort=add_time&order=desc
    //page=1&limit=20&couponId=31&sort=add_time&order=desc

    //TODO 返回值是啥 page=1&limit=20&couponId=17&userId=1&status=1&sort=add_time&order=desc
    @RequestMapping("/coupon/listuser")
    public BaseRespVO couponListUser(Integer page,Integer limit,Integer couponId,Integer userId,String status,String sort,String order){
        CouponListUserVO couponListUserVO = adService.getCouponList(page,limit,couponId,userId,status,sort,order);
        return BaseRespVO.ok(couponListUserVO);
    }

    //删除优惠卷
    @RequestMapping("/coupon/delete")
    @RequiresPermissions("admin:coupon:delete")
    public BaseRespVO couponDelete(@RequestBody Coupon coupon){
        adService.couponDelete(coupon);
        return BaseRespVO.ok(coupon);
    }
    //读取
    @RequestMapping("/coupon/read")
    @RequiresPermissions("admin:coupon:read")
    public BaseRespVO couponRead(int id){
        Coupon coupon = adService.readDetailById(id);
        return BaseRespVO.ok(coupon);
    }

    //--------------专题管理-----------------
    //获取专题列表
    @RequestMapping("/topic/list")
    @RequiresPermissions("admin:topic:list")
    public BaseRespVO topicList(Integer page,Integer limit,String sort,String order,String title,String subtitle){
        TopicListVO topicListVO = adService.selectTopicList(page,limit,sort,order,title,subtitle);
        return BaseRespVO.ok(topicListVO);
    }
    //增加专题
    @RequestMapping("/topic/create")
    @RequiresPermissions("admin:topic:create")
    public BaseRespVO topicCreate(@RequestBody Topic topic){
       Topic topic1 = adService.creatTopic(topic);
        return BaseRespVO.ok(topic1);
    }
    //更新专题
    @RequestMapping("/topic/update")
    @RequiresPermissions("admin:topic:update")
    public BaseRespVO topicUpdate(@RequestBody Topic topic){
        topic.setUpdateTime(new Date());

        int result = adService.updateTopic(topic);
        if (result == 200){
            return BaseRespVO.ok(topic);
        }else if (result == 404){
            return BaseRespVO.error("系统升级中",404);
        }else {
            return BaseRespVO.error("服务器升级中",500);
        }
    }
    //删除专题
    @RequestMapping("/topic/delete")
    @RequiresPermissions("admin:topic:delete")
    public BaseRespVO topicDelete(@RequestBody Topic topic){
        int result = adService.deleteTopic(topic);
        if (result == 200){
            return BaseRespVO.ok(topic);
        }else if (result == 404){
            return BaseRespVO.error("系统升级中",404);
        }else {
            return BaseRespVO.error("服务器升级中",500);
        }
    }
}
